home *** CD-ROM | disk | FTP | other *** search
- Imports ModulesDemo.Animals.Mammals
- Imports Acc = ModulesDemo.Computers.Accessories
-
- Imports Microsoft.VisualBasic.Compatibility.VB6
-
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestFactorial()
- 'TestBasicInheritance()
- 'TestImports()
- 'TestMultipleDims()
- 'TestShorthands()
- 'TestCollections()
- 'TestBlockVariables()
- 'TestDataTypes()
- 'TestCollections()
- 'TestFixedLenghtStrings()
- 'TestInitializers()
- 'TestReferenceTypes()
- 'TestEnums()
- 'TestArrays()
- 'TestArrayInitializers()
- 'TestArrayCloning()
- 'TestArrayLateBinding()
- 'TestCompareStructuresAndClasses()
-
- ' You need these statements when running inside Visual Studio, so that
- ' the Console window doesn't disappear
- Console.WriteLine("")
- Console.WriteLine(">>> press Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' this procedure tests the MathFunctions.Factorial function
-
- Sub TestFactorial()
- Console.WriteLine("Factorial(30) = " & CStr(Factorial(30)))
- End Sub
-
- ' this procedure tests the VB.NET collections support the default Item member
-
- Sub TestCollections()
- ' The .NET version of the Collection object
- Dim col As New Microsoft.VisualBasic.Collection()
- ' Add two elements.
- col.Add("Francesco", "FirstName")
- col.Add("Balena", "LastName")
- ' The following two statements are equivalent (Item is the default member).
- Console.WriteLine(col.Item(1)) ' => Francesco
- Console.WriteLine(col(1)) ' => Francesco
- ' The following two statements are equivalent (Item is the default member).
- Console.WriteLine(col.Item("LastName")) ' => Balena
- Console.WriteLine(col("LastName")) ' => Balena
- End Sub
-
- ' this procedure tests command math shorthands
-
- Sub TestShorthands()
- Dim x As Long = 9
- Dim y As Double = 6.8
-
- ' Increment x by one (same as x = x + 1).
- x += 1
- Console.WriteLine(x) ' => 10
- ' Decrement y by two (same as y = y รป 2).
- y -= 2
- Console.WriteLine(y) ' => 4.8
-
- ' Double x (same as x = x * 2).
- x *= 2
- Console.WriteLine(x) ' => 20
-
- ' Divide x by ten (same as x = x \ 10).
- x \= 10
- Console.WriteLine(x) ' => 2
-
- ' Divide y by four (same as y = y / 4).
- y /= 4
- Console.WriteLine(y) ' => 1.2
-
- ' Raise y to the 3rd power (same as y = y ^ 3).
- y ^= 3
- Console.WriteLine(y) ' => 1.728
-
- Dim s As String = "abc"
- ' Append a constant to a string (same as s = s & "ABC").
- s &= "ABC"
- Console.WriteLine(2) ' => abcABC
-
- ' test performance when incrementing an object field
- ' (compile this with overflow checks removed and enabled optimization)
- Dim i As Integer
- Dim obj As New Test()
- Dim start As Date
-
- start = Now
- For i = 1 To 1000000000
- obj.Value += 1
- Next
- Console.WriteLine("obj.Value += 1 {0} secs.", Now.Subtract(start))
-
- start = Now
- For i = 1 To 1000000000
- obj.Value = obj.Value + 1
- Next
- Console.WriteLine("obj.Value = obj.Value + 1 {0} secs.", Now.Subtract(start))
- End Sub
-
-
- ' This procedure tests using a base and an inherited class
-
- Sub TestBasicInheritance()
- ' create an use an object
- Dim aPerson As New Person()
- aPerson.FirstName = "Joe"
- aPerson.LastName = "Doe"
- Console.WriteLine(aPerson.Title & aPerson.CompleteName) ' => Mr. Joe Doe
-
- ' create and use an inherited object.
- Dim anEmployee As New Employee()
- With anEmployee
- .FirstName = "Robert"
- .LastName = "Smith"
- ' Use the new (non-inherited) members.
- .BirthDate = #2/5/1960#
- Console.WriteLine(.ReverseName) ' => Smith, Robert
- End With
-
- End Sub
-
- ' this procedure tests the syntax for Imports statements
-
- Sub TestImports()
- Dim m As Mouse ' same as Animals.Mammals.Mouse
- Dim m2 As Acc.Mouse ' same as Computers.Accessories.Mouse
- ' ...
- End Sub
-
- ' this procedure tests the syntax for declaratins of multiple variables
-
- Sub TestMultipleDims()
- ' Declare three Long variables.
- Dim x, y, z As Long
-
- ' Declare three variables of different type.
- Dim i As Long, k As Integer, s As String
- End Sub
-
- ' this procedure tests block-scoped variables
-
- Sub TestBlockVariables()
- Dim x, z As Integer
-
- ' NOTE: uncomment next line to prove that you can't have a block variable
- ' with the same name as a local variable
- 'Dim y As Integer ' Procedure variable with same name
-
- For x = 1 To 10
- Dim y As Integer ' A block variable
- ' ...
- Next
-
- ' this block proves that you can have two block variables with same name
- ' provided that their scope isn't nested
- For z = 1 To 10
- Dim y As Integer ' A block variable
- ' ...
- Next
-
- ' this block proves that inner block variables aren't reinitialized
- ' when the block is reentered.
- For z = 1 To 2
- ' ....
- For x = 1 To 2
- Dim y As Long
- y = y + 1
- Console.Write(y) ' => 1 2 3 4
- Console.Write(" ")
- Next
- Next
- Console.WriteLine("")
- End Sub
-
- ' this procedure contains several tests on data types
-
- Sub TestDataTypes()
- Dim x As Integer
-
- ' Increment X if it is less than 100.
- ' (This works also when Option Strict is On.)
- x = x - CInt(x < 100)
- Console.WriteLine(x) ' => 1
-
- ' Make it clear that you're adding a Decimal constant.
- Dim d As Decimal
- d = d + 123.45@
-
- Dim ch As Char
- ch = "A"c ' Note the trailing "c" character.
- ' *** The following line raises a compile error.
- ' ch = "ABC"c ' More than one character
-
- ch = CChar(Mid("Francesco", 3, 1))
- Console.WriteLine(ch) ' => a
-
- ch = Chr(65) ' This is character "A".
- Console.WriteLine(ch) ' => A
- End Sub
-
- ' this procedure tests fixed-length strings
-
- Sub TestFixedLenghtStrings()
- Dim s As New FixedLengthString(10)
- s.Value = "123456789012345" ' 15 characters
- ' The characters in excess have been truncated.
- Console.WriteLine(s.Value) ' => 1234567890
- End Sub
-
- ' this procedure tests initializers
-
- Sub TestInitializers()
- ' Two examples of variable initializers
- Dim width As Single = 1000
- Dim firstName As String = "Francesco"
-
- '*** Following line doesn't compile.
- ' Dim x, y, z As Long = 1
-
- ' you can use initializers with non-constant values
- Dim startDate As Date = Now()
-
- ' show that initializers can be used to re-initialize block variables
- Dim x, z As Integer
- For z = 1 To 2
- ' ....
- For x = 1 To 2
- ' Ensure that the Y variable always starts at zero.
- Dim y As Long = 0
- ' ...
- Next
- Next
- End Sub
-
- ' this procedure tests reference types and
- ' proves that Strings are a reference type
-
- Sub TestReferenceTypes()
- ' Person is a class defined in the current application.
- Dim p1 As New Person()
- p1.FirstName = "Francesco"
- ' Assign to another Person variable.
- Dim p2 As Person
- p2 = p1
- ' You can modify the original object through the new variable.
- p2.FirstName = "Joe"
- Console.WriteLine(p1.FirstName) ' => Joe
-
- ' prove that strings are reference types.
- Dim s1 As String = "Francesco"
- Dim s2 As String = s1
- ' Prove that the two variables point to the same String object.
- Console.WriteLine(s2 Is s1) ' => True
- End Sub
-
- ' This procedure tests Enum types
-
- Enum Shape
- Triangle ' This takes a zero value.
- Square ' 1
- Rectangle ' 2
- Circle ' 3
- Unknown = -999 ' (Values don't need to be sorted.)
- End Enum
-
- Sub TestEnums()
- ' A variable that can be assigned an Enum type.
- Dim aShape As Shape = Shape.Square
-
- aShape = CType(1, Shape)
- End Sub
-
- ' this procedure tests array operations
-
- Sub TestArrays()
- ' Declare the array.
- Dim arr() As Integer
- ' ...
- ' Create the array.
- ReDim arr(100) ' Note that no As clause is used here.
-
- ' Declare a two-dimensional array.
- Dim arr2(,) As String
- ' Declare a three-dimensional array.
- Dim arr3(,,) As String
- ' ...
- ' Create the arrays.
- ReDim arr2(10, 10)
- ReDim arr3(10, 10, 10)
-
- ReDim Preserve arr2(10, 20)
- ReDim Preserve arr3(10, 10, 20)
-
- '*** The following statements raise an
- ' ArrayTypeMismatchException exception at runtime.
- Try
-
- ReDim Preserve arr2(20, 10)
- ReDim Preserve arr3(10, 20, 20)
- Catch ex As Exception
- Console.WriteLine(ex.Message)
- End Try
- End Sub
-
- ' this procedure tests array initializers
-
- Sub TestArrayInitializers()
- ' Declare and create an array of 5 integers.
- Dim arr() As Integer = {0, 1, 2, 3, 4}
-
- ' Declare and create a two-dimensional array of strings
- ' with two rows and four columns.
- Dim arr2(,) As String = {{"00", "01", "02", "03"}, _
- {"10", "11", "12", "13"}}
- End Sub
-
- ' this procedure tests array cloning
-
- Sub TestArrayCloning()
- Dim arr1() As Integer = {0, 111, 222, 333}
- Dim arr2() As Integer
- ' Create another reference to the array.
- arr2 = arr1
- ' Modify the array through the second variable.
- arr2(1) = 9999
- ' Check that the original array has been modified.
- Console.WriteLine(arr1(1)) ' => 9999
-
- Dim arr3() As Integer = {0, 111, 222, 333}
- ' Create a copy (clone) of the array.
- Dim arr4() As Integer = CType(arr1.Clone, Integer())
- ' Modify an element in the new array.
- arr4(1) = 9999
- ' Check that the original array hasn't been affected.
- Console.WriteLine(arr3(1)) ' => 111
-
- ' Copy a 2-dimensional array
- Dim arr5(,) As Integer = {{0, 1, 2, 3}, {0, 10, 20, 30}}
- Dim arr6(,) As Integer
- ' Create a copy of the 2-dimensional array.
- arr6 = CType(arr5.Clone, Integer(,))
- Console.WriteLine(arr6(1, 1)) ' => 10
-
- ' you can always assign an array of reference types to an array of objects
- Dim strArr() As String = {"00", "11", "22", "33", "44"}
- Dim objArr() As Object = strArr
- Console.WriteLine(objArr(2)) ' => 22
- End Sub
-
- ' this procedure tests the different behavior of Structures and Classes
-
- Sub TestCompareStructuresAndClasses()
- ' Creation is similar, but structures don't require New.
- Dim cPers As New Person()
- Dim sPers As PersonStruct ' New is optional.
-
- ' Assignment to members is identical.
- cPers.FirstName = "Joe"
- cPers.LastName = "Doe"
- sPers.FirstName = "Joe"
- cPers.LastName = "Doe"
-
- ' Method and property invocation is also identical.
- Console.WriteLine(cPers.CompleteName()) ' => Joe Doe
- Console.WriteLine(sPers.CompleteName()) ' => Joe Doe
-
- ' Assignment to a variable of the same type has different effects.
-
- Dim cPers2 As Person = cPers
- ' Classes are reference types, hence the new variable receives
- ' a pointer to the original object.
- cPers2.FirstName = "Ann"
- ' The original object has been affected.
- Console.WriteLine(cPers.FirstName) ' => Ann
- '
- Dim sPers2 As PersonStruct = sPers
- ' Structure are value types, hence the new variable receives
- ' a copy of the original structure.
- sPers2.FirstName = "Ann"
- ' The original structure hasn't been affected
- Console.WriteLine(sPers.FirstName) ' => Joe
- End Sub
- End Module
-